/*
 * Copyright (c) 2022-2022 All rights Reserved.
 */

package com.driving.core.service.impl;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.springframework.stereotype.Service;

import com.driving.core.base.BaseServiceImpl;
import com.driving.core.entity.Admin;
import com.driving.core.entity.Coach;
import com.driving.core.entity.School;
import com.driving.core.exception.DrivingException;
import com.driving.core.mapper.IAdminMapper;
import com.driving.core.service.IAdminService;
import com.driving.core.service.dto.AdminDTO;
import com.driving.core.service.dto.CoachDTO;
import com.driving.core.service.dto.SchoolDTO;
import com.driving.core.util.Constant;
import com.driving.core.util.Constant.Audience;
import com.driving.core.util.JsonWebToken;
import com.driving.core.util.MessageType.ExceptionType;
import com.driving.core.util.PasswordEncryption;

import lombok.extern.slf4j.Slf4j;

/**
 * @author SAGITTAR
 * @since 2017
 */
@Slf4j
@Service(value = "adminService")
public class AdminServiceImpl extends BaseServiceImpl<IAdminMapper, Admin> implements IAdminService {

    @Override
    public boolean passwordUpdateOrReset(AdminDTO dto, String token) {
        int adminId = getCorrespondingIdByTokenOrMobile(baseMapper, token, dto.getMobile(), dto.getOldPassword());
        Admin admin = new Admin(adminId);
        try {
            admin.setPassword(PasswordEncryption.getEncryptedPassword(dto.getPassword()));
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            log.error(e.getLocalizedMessage(), e);
        }
        return updateById(admin);
    }

    @Override
    public Map<String, String> login(AdminDTO adminDTO) {
        Admin admin = baseMapper.selectByMobile(adminDTO.getMobile());
        if (Objects.isNull(admin)) {
            throw new DrivingException(configReader.getExceptionMessage(ExceptionType.USER_NOT_FOUND));
        }
        String encryptionPwd = admin.getPassword();
        if (!validatePassword(adminDTO.getPassword(), encryptionPwd)) {
            throw new DrivingException(configReader.getExceptionMessage(ExceptionType.INCORRECT_PASSWORD));
        }
        Map<String, String> loginResult = new HashMap<>(4);
        loginResult.put(Constant.NAME, admin.getName());
        String compact = JsonWebToken.compact(admin.getId(), Audience.ADMIN, admin.getMobile(), encryptionPwd);
        loginResult.put(Constant.TOKEN, compact);
        return loginResult;
    }

    @Override
    public Map<String, Object> addCoach(CoachDTO coachDTO) {
        Coach coach = coachDTO.toCoach();
        if (!coach.insert()) {
            return Collections.emptyMap();
        }
        return Collections.singletonMap(Constant.NAME, coach.getName());
    }

    @Override
    public Map<String, Object> addSchool(SchoolDTO schoolDTO) {
        School school = schoolDTO.toSchool();
        if (!school.insert()) {
            return Collections.emptyMap();
        }
        return Collections.singletonMap(Constant.NAME, school.getName());
    }
}
